package 每日一题;


import org.junit.Test;

import java.util.Arrays;

public class 分割链表 {

    public ListNode[] splitListToParts(ListNode head, int k) {
        int len=0;
        ListNode cur=head;
        while (cur!=null){
            len++;
            cur=cur.next;
        }
        ListNode[] nodes = new ListNode[k];
        int[] ints = new int[k];
        int i = len / k;
        int i1 = len % k;
        Arrays.fill(ints,i);
        for (int j = 0; j < i1; j++) {
            ints[j]++;
        }
        int idx=0;
        int count=1;
        while (head!=null){
            ListNode listNode = new ListNode(head.val);
            ListNode tem=listNode;
            for (int j = count; j < ints[idx]; j++) {
                ListNode listNode1 = new ListNode(head.next.val);
                tem.next=listNode1;
                head=head.next;
                tem=tem.next;
            }
            nodes[idx]=listNode;
            idx++;
            count=1;
            head=head.next;
        }
        return nodes;
    }
    @Test
    public void test(){
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        ListNode node6 = new ListNode(6);
        ListNode node7 = new ListNode(7);
        ListNode node8 = new ListNode(8);
        ListNode node9 = new ListNode(9);
        ListNode node10 = new ListNode(10);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        node5.next=node6;
        node6.next=node7;
        node7.next=node8;
        node8.next=node9;
        node9.next=node10;
        ListNode[] nodes = splitListToParts(node1, 3);
        for (int i = 0; i < 3; i++) {
            System.out.println(nodes[i].val);
        }
    }
}
